package com.admin.dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import com.admin.bean.LogOrderBean;

@Repository
public class LogOrderDao {

	@Autowired private JdbcTemplate jdbcTemplate;
	
	/**
	 * 插入新的临时订单表
	 * @param temp
	 * @return
	 */
	public int insertTempOrder(LogOrderBean log){
		String sql = "insert into log_order(telPhone,port,command,consume)values(?,?,?,?)";
		Object[] params = {log.getTelPhone(),log.getPort(),log.getCommand(),log.getConsume()};
		return jdbcTemplate.update(sql, params);
	}
	
	public int findCount(String telPhone){
		String sql = "select count(*) from log_order where 1=1";
		if(telPhone!=null && !"".equals(telPhone)){
			sql = sql + " and telPhone=?";
			return jdbcTemplate.queryForInt(sql, telPhone);
		}else{
			return jdbcTemplate.queryForInt(sql);
		}
	}
	
	public List<LogOrderBean> findByTelPhone(String telPhone,int start,int pageSize){
		String sql = "select * from log_order where 1=1";
		if(telPhone!=null && !"".equals(telPhone)){
			sql = sql + " and telPhone=? order by createTime desc limit ?,?";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<LogOrderBean>(LogOrderBean.class), new Object[]{telPhone,start,pageSize});
		}else{
			sql = sql + " order by createTime desc limit ?,?";
			return jdbcTemplate.query(sql, new BeanPropertyRowMapper<LogOrderBean>(LogOrderBean.class), new Object[]{start,pageSize});
		}
	}

	public int delByTelPhone(String telPhone){
		String sql = "delete from log_order where telPhone = ?";
		return jdbcTemplate.update(sql, telPhone);
	}
}
